package cn.itsource.product.controller;

import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.PageInfo;
import cn.itsource.product.domain.Product;
import cn.itsource.product.query.ProductQuery;
import cn.itsource.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//服务管理controller层
@RestController
@RequestMapping("/product")
@Api(tags = "服务管理接口",description = "服务管理接口详细描述")
public class ProductController {
    @Autowired
    private IProductService productService;
  
    /**
     *
     * @param product   有ID是修改，没得是保存
     * @return          @RequestBody：前端传入的数据时json必须加此注解
     */
    @PostMapping
    @ApiOperation(value = "服务管理添加或修改",notes = "如果有id就是修改否则就是添加")
    public AxajResult save(@RequestBody Product product) {
        try {
            productService.save(product);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    /**
     *
     * @param id    通过id删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "服务管理删除",notes = "很好用的接口")
    public AxajResult delete(@PathVariable("id")Long id) {
        try {
            productService.delete(id);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }



    /**
     * 查询所有的服务管理对象
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "分页查询服务管理",notes = "很好用的接口")
    public PageInfo<Product> findAll(@RequestBody ProductQuery productQuery){
        //分页查询
        return  productService.findAllByQuery(productQuery);
    }
    /**
     * 批量删除
     */
    @PatchMapping("/delete")
    @ApiOperation(value = "服务管理删除",notes = "很好用的接口")
    public AxajResult batchDelete(@RequestBody List<Product> products) {
        try {
            productService.batchDelete(products);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    /**
     * 批量上架
     */
    @PatchMapping("/onsale")
    @ApiOperation(value = "服务管理删除",notes = "很好用的接口")
    public AxajResult onSale(@RequestBody List<Product> products) {
        try {
            productService.onSale(products);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    /**
     * 批量下架
     */
    @PatchMapping("/offsale")
    @ApiOperation(value = "服务管理删除",notes = "很好用的接口")
    public AxajResult offSale(@RequestBody List<Product> products) {
        try {
            productService.offSale(products);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    /**
     * 前台展示查询所有的服务管理对象
     * @return
     */
    @PatchMapping("findHomePageByQuery")
    @ApiOperation(value = "分页查询服务管理",notes = "很好用的接口")
    public PageInfo<Product> findHomePageByQuery(@RequestBody ProductQuery productQuery){
        //分页查询
        return  productService.findAllByQuery(productQuery);
    }

    /**
     * 查询单个服务对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product findProductById(@PathVariable("id") Long id){
            return  productService.findProductById(id);
    }
}
